// BatchTreeCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "BatchTreeCtrl.h"
#include "BatchTreeItem.h"
#include "Resource.h"

// CBatchTreeCtrl

IMPLEMENT_DYNAMIC(CBatchTreeCtrl, CTreeCtrl)

CBatchTreeCtrl::CBatchTreeCtrl() : m_hRoot(NULL)
{	
	CTreeCtrl::CTreeCtrl();
}

BOOL CBatchTreeCtrl:: Create(_In_ DWORD dwStyle, _In_ const RECT& rect, _In_ CWnd* pParentWnd, _In_ UINT nID)
{
	BOOL retValue =	CTreeCtrl::Create( dwStyle , rect , pParentWnd , nID );
	
	if(retValue)
	{	
		m_imageList.DeleteImageList();
		CBitmap bmp;
		if (!bmp.LoadBitmap(IDB_TREEIMAGES))
		{
			TRACE(_T("Can't load bitmap: %x\n"), IDB_TREEIMAGES);
		}

		m_imageList.Create(32, 32, ILC_COLOR24 | ILC_MASK, 0, 0);
		m_imageList.Add(&bmp, RGB(255, 0, 255));
		int i =m_imageList.GetImageCount();
		this->SetImageList(&m_imageList, TVSIL_NORMAL);	
		bmp.DeleteObject();
		Init();
	}
	else
	{
		TRACE( _T("Tree control failed to create!") ) ;
	}

	return retValue;
}

void CBatchTreeCtrl::Init()
{
	BeginWaitCursor() ;

	this->DeleteAllItems() ;
	
	CBatchTreeItem* pItem = new CBatchTreeItem(CBatchTreeItem::BATCH) ;

	TV_INSERTSTRUCT tvis;
	ZeroMemory(&tvis, sizeof(TV_INSERTSTRUCT));	
	tvis.hParent = TVI_ROOT ;
	tvis.hInsertAfter = TVI_FIRST ;
	tvis.item.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE ;
	tvis.item.lParam = (LPARAM)pItem ;
	tvis.item.pszText		 =  _T( "Batch");
	tvis.item.iImage		 = CBatchTreeItem::BATCH;
	tvis.item.iSelectedImage = CBatchTreeItem::BATCH;	
	tvis.item.cChildren		 = TRUE;
	this->m_hRoot = this->InsertItem(&tvis);	
	this->SetRedraw( TRUE ) ;	
	EndWaitCursor() ;
}

HTREEITEM CBatchTreeCtrl::InsertBatchItem(CBatch batch)
{	
	BeginWaitCursor() ;
	this->SetRedraw( FALSE ) ;
	CBatchTreeItem* pItem = new CBatchTreeItem(batch,CBatchTreeItem::DOCUMENT) ;

	TV_INSERTSTRUCT tvis;
	ZeroMemory(&tvis, sizeof(TV_INSERTSTRUCT));	
	tvis.hParent = this->m_hRoot;
	tvis.hInsertAfter = TVI_LAST ;
	tvis.item.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE ;
	tvis.item.lParam = (LPARAM)pItem ;
	tvis.item.pszText		 =  _T( "Document" );
	tvis.item.iImage		 = CBatchTreeItem::DOCUMENT;
	tvis.item.iSelectedImage = CBatchTreeItem::DOCUMENT;
	tvis.item.cChildren		 = TRUE;
	
	this->SetRedraw( TRUE ) ;
	EndWaitCursor() ;
	HTREEITEM  m_hBatchItem =  this->InsertItem(&tvis);
	//this->SelectItem(m_hBatchItem);
	this->Expand( this->m_hRoot, TVE_EXPAND ) ;
	this->Expand( m_hBatchItem, TVE_EXPAND ) ;
	
	return m_hBatchItem;
}

HTREEITEM CBatchTreeCtrl::InsertPageItem(HTREEITEM hParentItem , int index)
{	
	BeginWaitCursor() ;
	this->SetRedraw( FALSE ) ;
	CBatchTreeItem* pItem = new CBatchTreeItem(index,CBatchTreeItem::PAGE) ;

	TV_INSERTSTRUCT tvis;
	ZeroMemory(&tvis, sizeof(TV_INSERTSTRUCT));	
	tvis.hParent = hParentItem ;
	tvis.hInsertAfter = TVI_LAST ;
	tvis.item.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE ;
	tvis.item.lParam = (LPARAM)pItem ;
	
	CString strPageTitle;
	strPageTitle.Format(L"%d. Page",index+1);

	int length = 1024;
	LPWSTR pwsz = strPageTitle.GetBuffer(length);
	strPageTitle.ReleaseBuffer();

 
	tvis.item.pszText		 = pwsz;
	tvis.item.iImage		 = CBatchTreeItem::PAGE;
	tvis.item.iSelectedImage = CBatchTreeItem::PAGE;
	tvis.item.cChildren		 = FALSE;
	
	this->SetRedraw( TRUE ) ;
	EndWaitCursor() ;
	HTREEITEM  m_hPageItem =  this->InsertItem(&tvis);
	
	this->Expand( hParentItem, TVE_EXPAND ) ;
	this->Expand( m_hPageItem, TVE_EXPAND ) ;
	
	return m_hPageItem;
}

void CBatchTreeCtrl::OnTreeSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
	NM_TREEVIEW* pnmtv = (NM_TREEVIEW*)pNMHDR;
	CBatchTreeItem*  pItem = (CBatchTreeItem*)pnmtv->itemNew.lParam ;
	try
	{
		if (pItem == NULL)
			return ;
		if(pItem->GetValueType() ==  pItem->BATCH)
		{
			::SendMessageA(this->GetParentFrame()->m_hWnd,ID_TREEVIEW_CHANGE_ITEM, NULL, pItem->BATCH);	
		}
		else if(pItem->GetValueType() == pItem->DOCUMENT)
		{		
			HTREEITEM hParentItem = GetParentItem(pnmtv->itemNew.hItem);
			if (hParentItem)
			{
				CBatch* _batch = &(pItem->GetValue());
				_batch->SetIndex(0); //default page...
				::SendMessageA(this->GetParentFrame()->m_hWnd,ID_TREEVIEW_CHANGE_ITEM, reinterpret_cast<WPARAM>(_batch), pItem->DOCUMENT);			
			}
		}
		else if(pItem->GetValueType() ==  pItem->PAGE)
		{	
			HTREEITEM hParentItem = GetParentItem(pnmtv->itemNew.hItem);
			if (hParentItem)
			{
				CBatchTreeItem*  pParentItem = (CBatchTreeItem*)GetItemData(hParentItem);
				CBatch* _batch = &(pParentItem->GetValue());
				_batch->SetIndex(pItem->GetIndex());
				::SendMessageA(this->GetParentFrame()->m_hWnd,ID_TREEVIEW_CHANGE_ITEM, reinterpret_cast<WPARAM>(_batch), pItem->PAGE);			
			}
		}
		else
		{
			//...
		}
	}
	catch(...)
	{

	}

	*pResult = 0;
}

void CBatchTreeCtrl::OnTreeItemExpanding(NMHDR* pNMHDR, LRESULT* pResult)
{
	BeginWaitCursor() ;
	this->SetRedraw( FALSE ) ;
	NM_TREEVIEW* pnmtv = (NM_TREEVIEW*)pNMHDR;	
    HTREEITEM    hItem       = pnmtv->itemNew.hItem;
	CBatchTreeItem*  pItem = (CBatchTreeItem*)pnmtv->itemNew.lParam ;	
	UINT         action      = pnmtv->action;
	if (pItem == NULL)
		return ;
	if(pItem->GetValueType() == pItem->DOCUMENT)
	{		
		if(action & TVE_COLLAPSE)
		{
			 this->SetItemImage(hItem,3,3);
		}
		if(action & TVE_EXPAND)
		{
			 this->SetItemImage(hItem,(int)  CBatchTreeItem::DOCUMENT, (int) CBatchTreeItem::DOCUMENT);
		}
	}
	
	this->SetRedraw( TRUE ) ;
	EndWaitCursor() ;
	 *pResult = 0;
}

CBatchTreeCtrl::~CBatchTreeCtrl()
{
	CTreeCtrl::~CTreeCtrl();
}


void CBatchTreeCtrl::OnDestroy()
{
	CTreeCtrl::OnDestroy();
	this->DeleteAllItems() ;
}
void CBatchTreeCtrl::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{

}

BEGIN_MESSAGE_MAP(CBatchTreeCtrl, CTreeCtrl)
	ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnTreeSelchanged)
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnTreeItemExpanding)
	ON_WM_DESTROY()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_KILLFOCUS()
	ON_WM_CREATE()
	ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()


// CBatchTreeCtrl message handlers


